﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Common.Constants;
using Common.Helper;
using Entities;

namespace DataAccess
{
   public class CountyDataAccess
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static CountyDataAccess Instance { get; private set; }

        /// <summary>
        /// Initializes the <see cref="AddressDataAccess" /> class.
        /// </summary>
        static CountyDataAccess() { Instance = new CountyDataAccess(); }



        /// <summary>
        /// Extracts the raw data.
        /// </summary>
        /// <param name="dataReader">The data reader.</param>
        /// <returns></returns>
        private static County ExtractRawData(IDataRecord dataReader)
        {
            var county = new County();
                   
            
            if (dataReader["CountyId"] != null)
                county.CountyId = (Guid)dataReader["CountyId"];
            if (dataReader["CountryId"] != null)
                county.CountryId = (Guid)dataReader["CountryId"];
            if (dataReader["GovOfficeRegionId"] != null)
                county.GovOfficeRegionId = (Guid)dataReader["GovOfficeRegionId"];
            county.CountyName = dataReader["CountyName"].ToString();
            return county;


        }
        public Collection<County> GetCountyByGovOfficeRegion(string govOfficeRegionId)
        {
            var collectParameters = new Collection<SqlParameter>
                                        {
                                            new SqlParameter("govOfficeRegionId", govOfficeRegionId)
                                        };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetCountyByGovOfficeRegion, collectParameters))
            {
                var listCounty = new Collection<County>();
                while (reader.Read())
                {
                    var result = ExtractRawData(reader);

                    listCounty.Add(result);
                }
                return listCounty;
            }
        }
        public County GetCounty(string countyId)
        {


            var colParameter = new Collection<SqlParameter>
                                   {
                                       new SqlParameter("CountyId", countyId)
                                      
                                       
                                   };
            using (var reader = SqlHelper.ExecuteReader(ProcedureStores.GetCounty, colParameter))
            {
                if (reader.Read())
                {
                    return ExtractRawData(reader);

                }
                return null;
            }
        }
        public int Insert(County county)
        {
            var colParameter = new Collection<SqlParameter>
                    {
                       new SqlParameter("CountryId",county.CountyId),
                       new SqlParameter("CountryId",county.CountryId),
                       new SqlParameter("CountyName",county.CountyName)
                     
                       
                    };

            var resuft = SqlHelper.ExecuteNonQuery(ProcedureStores.InsertCounty, colParameter);
            return resuft;
        }


    }
}
